﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using UAC;
using UAC.Operation;

namespace UACManager
{
    public partial class ControlConditionForm : Form
    {
        private ControlCondition condition;
        private int documentDefID;
        public ControlConditionForm(int documentDefID, ControlCondition condition)
        {
            InitializeComponent();
            this.condition = condition;
            this.documentDefID = documentDefID;
        }

        private void ControlConditionForm_Load(object sender, EventArgs e)
        {
            if (this.condition == null)
                this.Text = "新建控制条件";
            else
                this.Text = string.Format("编辑控制条件 - {0}", this.condition.DocumentDefID);
            LoadControlCondition();
        }

        private void LoadControlCondition()
        {
            this.comboBoxOperationType.Items.AddRange(new object[] { OperationType.And, OperationType.Or, OperationType.Person, OperationType.Role, OperationType.Compare });
            this.OperationTypeChange(OperationType.And);
            if (this.condition != null)
            {
                TreeNode node = new TreeNode() { Text = string.Format("【{0}】{1}", this.condition.OperationType, this.condition.Description) };
                node.Tag = this.condition;
                treeViewMain.Nodes.Add(node);
                AppendNodes(node, this.condition);
            }
        }

        private ControlCondition conditionSelected;
        private TreeNode treeNodeSelected;
        private void AppendNodes(TreeNode parentNode, ControlCondition condition)
        {
            if (condition.ConditionList != null)
            {
                foreach (ControlCondition child in condition.ConditionList)
                {
                    TreeNode node = new TreeNode() { Text = string.Format("【{0}】{1}", child.OperationType, child.Description) };
                    node.Tag = child;
                    parentNode.Nodes.Add(node);
                    AppendNodes(node, child);
                }
            }
        }

        private void treeViewMain_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (treeViewMain.SelectedNode != null)
            {
                treeNodeSelected = treeViewMain.SelectedNode;
                ControlCondition _condition = treeViewMain.SelectedNode.Tag as ControlCondition;
                ShowAfterSelected(_condition);
            }
        }

        private void comboBoxOperationType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBoxOperationType.SelectedItem != null)
            {
                OperationType operationType = (OperationType)comboBoxOperationType.SelectedItem;
                this.OperationTypeChange(operationType);
            }
        }

        #region OperationTypeChange
        private void OperationTypeChange(OperationType operationType)
        {
            switch (operationType)
            {
                case OperationType.And:
                    {
                        comboBoxType1.Visible = false;
                        comboBoxType2.Visible = false;
                        tValue1.Visible = false;
                        tValue2.Visible = false;
                        comboBoxValue3.Visible = false;
                        comboBoxValue4.Visible = false;
                    }
                    break;
                case OperationType.Or:
                    {
                        comboBoxType1.Visible = false;
                        comboBoxType2.Visible = false;
                        tValue1.Visible = false;
                        tValue2.Visible = false;
                        comboBoxValue3.Visible = false;
                        comboBoxValue4.Visible = false;
                    }
                    break;
                case OperationType.Person:
                    {
                        comboBoxType1.Visible = true;
                        comboBoxType2.Visible = false;
                        tValue1.Visible = true;
                        tValue2.Visible = false;
                        comboBoxValue3.Visible = false;
                        comboBoxValue4.Visible = false;
                    }
                    break;
                case OperationType.Role:
                    {
                        comboBoxType1.Visible = true;
                        comboBoxType2.Visible = true;
                        tValue1.Visible = true;
                        tValue2.Visible = true;
                        comboBoxValue3.Visible = false;
                        comboBoxValue4.Visible = false;
                    }
                    break;
                case OperationType.Compare:
                    {
                        comboBoxType1.Visible = true;
                        comboBoxType2.Visible = true;
                        tValue1.Visible = true;
                        tValue2.Visible = true;
                        comboBoxValue3.Visible = true;
                        comboBoxValue4.Visible = true;
                    }
                    break;
            }
        }
        #endregion

        private void ShowAfterSelected(ControlCondition condition)
        {
            this.conditionSelected = condition;
            tDesc.Text = condition.Description;
            this.comboBoxOperationType.SelectedItem = condition.OperationType;
            this.OperationTypeChange(condition.OperationType);
            switch (condition.OperationType)
            {
                case OperationType.Person:
                    {
                        comboBoxType1.Items.Clear();

                        List<KeyValue> vals = new List<KeyValue>();
                        vals.Add(new KeyValue() { Text = "任意的", Value = UAC.Operation.ValueType.Arbitrary });
                        vals.Add(new KeyValue() { Text = "常量", Value = UAC.Operation.ValueType.Constant });
                        comboBoxType1.Items.AddRange(vals.ToArray());

                        KeyValue val = vals.Find((_val) =>
                        {
                            return (UAC.Operation.ValueType)_val.Value == condition.Values1.ValueType;
                        });

                        comboBoxType1.SelectedItem = val;

                        switch (condition.Values1.ValueType)
                        {
                            case UAC.Operation.ValueType.Arbitrary:
                                tValue1.Visible = false;
                                break;
                            case UAC.Operation.ValueType.Constant:
                                tValue1.Visible = true;
                                tValue1.Text = condition.Values1.Value.ToString();
                                break;
                        }

                        comboBoxType2.Visible = false;
                        tValue2.Visible = false;
                    }
                    break;
                case OperationType.Role:
                    {
                        comboBoxType1.Items.Clear();

                        List<KeyValue> vals = new List<KeyValue>();
                        vals.Add(new KeyValue() { Text = "常量", Value = UAC.Operation.ValueType.Constant });
                        comboBoxType1.Items.AddRange(vals.ToArray());

                        KeyValue val = vals.Find((_val) =>
                        {
                            return (UAC.Operation.ValueType)_val.Value == condition.Values1.ValueType;
                        });

                        comboBoxType1.SelectedItem = val;

                        switch (condition.Values1.ValueType)
                        {
                            case UAC.Operation.ValueType.Arbitrary:
                                tValue1.Visible = false;
                                break;
                            case UAC.Operation.ValueType.Constant:
                                tValue1.Visible = true;
                                tValue1.Text = condition.Values1.Value.ToString();
                                break;
                        }

                        comboBoxType2.Items.Clear();

                        List<KeyValue> vals2 = new List<KeyValue>();
                        vals2.Add(new KeyValue() { Text = "常量", Value = UAC.Operation.ValueType.Constant });
                        comboBoxType2.Items.AddRange(vals2.ToArray());

                        KeyValue val2 = vals2.Find((_val) =>
                        {
                            return (UAC.Operation.ValueType)_val.Value == condition.Values2.ValueType;
                        });

                        comboBoxType2.SelectedItem = val2;

                        switch (condition.Values2.ValueType)
                        {
                            case UAC.Operation.ValueType.Arbitrary:
                                tValue2.Visible = false;
                                break;
                            case UAC.Operation.ValueType.Constant:
                                tValue2.Visible = true;
                                tValue2.Text = condition.Values2.Value.ToString();
                                break;
                        }
                    }
                    break;
                case OperationType.Compare:
                    {
                        comboBoxType1.Items.Clear();

                        List<KeyValue> vals = new List<KeyValue>();
                        vals.Add(new KeyValue() { Text = "常量", Value = UAC.Operation.ValueType.Constant });
                        vals.Add(new KeyValue() { Text = "文档变量", Value = UAC.Operation.ValueType.DocDef });
                        comboBoxType1.Items.AddRange(vals.ToArray());

                        KeyValue val = vals.Find((_val) =>
                        {
                            return (UAC.Operation.ValueType)_val.Value == condition.Values1.ValueType;
                        });

                        comboBoxType1.SelectedItem = val;

                        switch (condition.Values1.ValueType)
                        {
                            case UAC.Operation.ValueType.Arbitrary:
                                tValue1.Visible = false;
                                break;
                            case UAC.Operation.ValueType.Constant:
                                tValue1.Visible = true;
                                tValue1.Text = condition.Values1.Value.ToString();
                                break;
                            case UAC.Operation.ValueType.DocDef:
                                tValue1.Visible = true;
                                tValue1.Text = condition.Values1.Value.ToString();
                                break;
                        }

                        comboBoxType2.Items.Clear();

                        List<KeyValue> vals2 = new List<KeyValue>();
                        vals2.Add(new KeyValue() { Text = "常量", Value = UAC.Operation.ValueType.Constant });
                        vals2.Add(new KeyValue() { Text = "文档变量", Value = UAC.Operation.ValueType.DocDef });
                        comboBoxType2.Items.AddRange(vals2.ToArray());

                        KeyValue val2 = vals2.Find((_val) =>
                        {
                            return (UAC.Operation.ValueType)_val.Value == condition.Values2.ValueType;
                        });

                        comboBoxType2.SelectedItem = val2;

                        switch (condition.Values2.ValueType)
                        {
                            case UAC.Operation.ValueType.Arbitrary:
                                tValue2.Visible = false;
                                break;
                            case UAC.Operation.ValueType.Constant:
                                tValue2.Visible = true;
                                tValue2.Text = condition.Values2.Value.ToString();
                                break;
                            case UAC.Operation.ValueType.DocDef:
                                tValue2.Visible = true;
                                tValue2.Text = condition.Values2.Value.ToString();
                                break;
                        }

                        comboBoxValue3.Items.Clear();
                        List<KeyValue> vals3 = new List<KeyValue>();
                        vals3.Add(new KeyValue() { Text = "=", Value = UAC.Operation.CompareType.Equal });
                        vals3.Add(new KeyValue() { Text = ">", Value = UAC.Operation.CompareType.Greater });
                        vals3.Add(new KeyValue() { Text = "≥", Value = UAC.Operation.CompareType.GreaterOrEqual });
                        vals3.Add(new KeyValue() { Text = "<", Value = UAC.Operation.CompareType.Less });
                        vals3.Add(new KeyValue() { Text = "≤", Value = UAC.Operation.CompareType.LessOrEqual });
                        vals3.Add(new KeyValue() { Text = "≠", Value = UAC.Operation.CompareType.Unequal });
                        comboBoxValue3.Items.AddRange(vals3.ToArray());

                        KeyValue val3 = vals3.Find((_val) =>
                        {
                            return (UAC.Operation.CompareType)_val.Value == (CompareType)Enum.Parse(typeof(CompareType), condition.Values3.Value.ToString());
                        });
                        comboBoxValue3.SelectedItem = val3;

                        comboBoxValue4.Items.Clear();
                        List<KeyValue> vals4 = new List<KeyValue>();
                        vals4.Add(new KeyValue() { Text = "Date", Value = UAC.Operation.ParamType.Date });
                        vals4.Add(new KeyValue() { Text = "Number", Value = UAC.Operation.ParamType.Number });
                        vals4.Add(new KeyValue() { Text = "String", Value = UAC.Operation.ParamType.String });
                        comboBoxValue4.Items.AddRange(vals4.ToArray());

                        KeyValue val4 = vals4.Find((_val) =>
                        {
                            return (UAC.Operation.ParamType)_val.Value == (ParamType)Enum.Parse(typeof(ParamType), condition.Values4.Value.ToString());
                        });
                        comboBoxValue4.SelectedItem = val4;
                    }
                    break;
            }
        }

        private void comboBoxType1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBoxType1.SelectedItem != null)
            {
                KeyValue val = comboBoxType1.SelectedItem as KeyValue;
                if (val != null)
                {
                    UAC.Operation.ValueType valType = (UAC.Operation.ValueType)val.Value;
                    tValue1.Visible = (valType != UAC.Operation.ValueType.Arbitrary);
                }
            }
        }

        private void comboBoxType2_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBoxType2.SelectedItem != null)
            {
                KeyValue val = comboBoxType2.SelectedItem as KeyValue;
                if (val != null)
                {
                    UAC.Operation.ValueType valType = (UAC.Operation.ValueType)val.Value;
                    tValue2.Visible = (valType != UAC.Operation.ValueType.Arbitrary);
                }
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            updateToSave();
        }

        private void updateToSave()
        {
            try
            {
                var v = this.conditionSelected;
                //treeNodeSelected
                Utils.UpdateOperationControl(this.conditionSelected);
            }
            catch (Exception)
            {
            }
        }
    }
}
